package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.system.domain.MesMatbom;
import com.ruoyi.system.domain.MesStock;
import com.ruoyi.system.mapper.MesStockMapper;
import com.ruoyi.system.service.IMesStockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.MesBuyInMapper;
import com.ruoyi.system.domain.MesBuyIn;
import com.ruoyi.system.service.IMesBuyInService;

/**
 * 采购入库Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-06-01
 */
@Service
public class MesBuyInServiceImpl implements IMesBuyInService 
{
    @Autowired
    private MesBuyInMapper mesBuyInMapper;
    @Autowired
    private IMesStockService mesStockService;
    @Autowired
    private MesStockMapper mesStockMapper;
    /**
     * 查询采购入库
     * 
     * @param id 采购入库主键
     * @return 采购入库
     */
    @Override
    public MesBuyIn selectMesBuyInById(Integer id)
    {
        return mesBuyInMapper.selectMesBuyInById(id);
    }

    /**
     * 查询采购入库列表
     * 
     * @param mesBuyIn 采购入库
     * @return 采购入库
     */
    @Override
    public List<MesBuyIn> selectMesBuyInList(MesBuyIn mesBuyIn)
    {
        return mesBuyInMapper.selectMesBuyInList(mesBuyIn);
    }

    /**
     * 新增采购入库
     * 
     * @param mesBuyIn 采购入库
     * @return 结果
     */
    @Override
    public int insertMesBuyIn(MesBuyIn mesBuyIn)
    {
        List<MesBuyIn> mtrs = mesBuyIn.getMtrs();

        for (MesBuyIn mtr : mtrs) {
            //1：处理数据
            mtr.setBuyOrdId(mtr.getBuyOrdId());//原料编码
            mtr.setBuyInTime(mtr.getBuyInTime());
            mtr.setMatId(mtr.getMatId());
            mtr.setNeedMatNum(mtr.getNeedMatNum());//入库数
            mtr.setLocaId(mtr.getLocaId());
            //2.sql插入
            mesBuyInMapper.insertMesBuyIn(mtr);

            //3.更新库存数量
            MesStock stock = mesStockMapper.selectByItemId(mtr.getMatId());
            if (stock != null){
                stock.setStockNum(stock.getStockNum() + mtr.getNeedMatNum());
                mesStockService.updateMesStock(stock);
            }
        }
        return 1;
        //return mesBuyInMapper.insertMesBuyIn(mesBuyIn);
    }

    /**
     * 修改采购入库
     * 
     * @param mesBuyIn 采购入库
     * @return 结果
     */
    @Override
    public int updateMesBuyIn(MesBuyIn mesBuyIn)
    {
        return mesBuyInMapper.updateMesBuyIn(mesBuyIn);
    }

    /**
     * 批量删除采购入库
     * 
     * @param ids 需要删除的采购入库主键
     * @return 结果
     */
    @Override
    public int deleteMesBuyInByIds(Integer[] ids)
    {
        return mesBuyInMapper.deleteMesBuyInByIds(ids);
    }

    /**
     * 删除采购入库信息
     * 
     * @param id 采购入库主键
     * @return 结果
     */
    @Override
    public int deleteMesBuyInById(Integer id)
    {
        return mesBuyInMapper.deleteMesBuyInById(id);
    }


    //查当前库存量 1

    //199+1
}
